package com.ruoyi.project.data.controller;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.data.domain.WeightPos;
import com.ruoyi.project.data.service.IWeightPosService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 商户信息Controller
 *
 * @author gwc
 * @date 2020-03-27
 */
@RestController
@RequestMapping("/data/pos")
public class WeightPosController extends BaseController
{
    @Autowired
    private IWeightPosService weightPosService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询商户信息列表
     */
    @PreAuthorize("@ss.hasPermi('data:pos:list')")
    @GetMapping("/list")
    public TableDataInfo list(WeightPos weightPos)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        weightPos.setStationId(stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightPos.setAreas(areas);
        weightPos.setManagementCharacters(managementCharacters);
        startPage();
        List<WeightPos> list = weightPosService.selectWeightPosList(weightPos);
        return getDataTable(list);
    }

    /**
     * 导出商户信息列表
     */
    @PreAuthorize("@ss.hasPermi('data:pos:export')")
    @Log(title = "商户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WeightPos weightPos)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        weightPos.setStationId(stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightPos.setAreas(areas);
        weightPos.setManagementCharacters(managementCharacters);
        List<WeightPos> list = weightPosService.selectWeightPosList(weightPos);
        ExcelUtil<WeightPos> util = new ExcelUtil<WeightPos>(WeightPos.class);
        return util.exportExcel(list, "pos");
    }

    /**
     * 获取商户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('data:pos:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(weightPosService.selectWeightPosById(id));
    }

    /**
     * 新增商户信息
     */
    @PreAuthorize("@ss.hasPermi('data:pos:add')")
    @Log(title = "商户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WeightPos weightPos)
    {
        if (UserConstants.NOT_UNIQUE.equals(weightPosService.checkPosNameUnique(weightPos)))
        {
            return AjaxResult.error("新增商户失败，该商户已存在");
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station")
                || permissionService.hasRole("xq_station") || permissionService.hasRole("jy_station") || permissionService.hasRole("yx_station"))){
            Long stationId = user.getStationId();
            weightPos.setStationId(stationId);
        }
        weightPos.setCreateId(user.getUserId().toString());
        return toAjax(weightPosService.insertWeightPos(weightPos));
    }

    /**
     * 修改商户信息
     */
    @PreAuthorize("@ss.hasPermi('data:pos:edit')")
    @Log(title = "商户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WeightPos weightPos)
    {
        if (UserConstants.NOT_UNIQUE.equals(weightPosService.checkPosNameUnique(weightPos)))
        {
            return AjaxResult.error("修改商户失败，该商户已存在");
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station")
                || permissionService.hasRole("xq_station") || permissionService.hasRole("jy_station") || permissionService.hasRole("yx_station"))){
            Long stationId = user.getStationId();
            weightPos.setStationId(stationId);
        }
        weightPos.setUpdateId(user.getUserId().toString());
        return toAjax(weightPosService.updateWeightPos(weightPos));
    }

    /**
     * 删除商户信息
     */
    @PreAuthorize("@ss.hasPermi('data:pos:remove')")
    @Log(title = "商户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(weightPosService.deleteWeightPosByIds(ids));
    }
}
